package com.mrd.gtimpframe.action;

import com.mrd.base.Jqgrid;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimpframe.entity.Declaration;
import com.mrd.gtimpframe.entity.RejectRecord;
import com.mrd.gtimpframe.service.DeclarationService;
import com.mrd.gtimpframe.service.RejectRecordService;
import com.mrd.gtimpframe.service.RejectService;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by dusy on 2017/7/28.
 */
@Controller
@RequestMapping("/business/reject")
public class RejectController {
    @Autowired
    private RejectService rejectService;
    @Autowired
    private RejectRecordService rejectRecordService;
    @Autowired
    private DeclarationService declarationService;

    private Logger logger = LoggerFactory.getLogger(RejectController.class);

    /**
     * 申请驳回页面
     */
    @RequestMapping("/customer/page")
    public String rejectPage(String declaId, Model model) {
        model.addAttribute("declaId", declaId);
        return "/gtimpframe/business/customerReject";
    }

    /**
     * 客户申请驳回
     */
    @RequestMapping("/customer/apply")
    @ResponseBody
    public Result customerApply(String declaId, String applyReasonId, String remark) {
        if (StringUtils.isEmpty(declaId)) {
            return Result.newFailInstance("请选择需要驳回的报关单");
        }
        if (StringUtils.isEmpty(applyReasonId)) {
            return Result.newFailInstance("请选择申请驳回的原因");
        }
        try {
            return rejectService.customerApply(declaId, applyReasonId, remark);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("客户申请驳回出错", e);
            return Result.newFailInstance(e.getMessage());
        }
    }

    /**
     * 处理驳回页面
     */
    @RequestMapping("/supplier/page")
    public String acceptPage(String declaId, Model model) {
        model.addAttribute("declaId", declaId);
        return "/gtimpframe/business/acceptReject";
    }

    /**
     * 查看海关退单原因页面
     */
    @RequestMapping("/customer/checkChargeback")
    public String checkChargeback(String declaId, Model model) {
        RejectRecord rejectRecord = new RejectRecord();
        rejectRecord.setDid(declaId);
        rejectRecord.setAcceptedResult("CHARGEBACK");
        List<RejectRecord> rejectRecords = rejectRecordService.selectSelective(rejectRecord);
        model.addAttribute("rejectRecord", rejectRecords.get(0));
        return "/gtimpframe/business/checkChargeback";
    }

    /**
     * 海关退单页面
     */
    @RequestMapping("/supplier/chargeBackPage")
    public String chargeBackPage(String declaId, Model model) {
        model.addAttribute("declaId", declaId);
        return "/gtimpframe/business/chargeBack";
    }

    /**
     * 供应商处理驳回申请
     */
    @RequestMapping("/supplier/handle")
    @ResponseBody
    public Result supplierAccept(String declaId, String acceptedResult, String dealWithNpReason, String acceptedOpinion) {
        if (StringUtils.isEmpty(declaId)) {
            return Result.newFailInstance("请选择需要处理的报关单");
        }
        if (StringUtils.isEmpty(acceptedResult)) {
            return Result.newFailInstance("请选择驳回结果");
        }
        try {
            return rejectService.supplierAccept(declaId, acceptedResult, dealWithNpReason, acceptedOpinion);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("供应商处理驳回申请失败", e);
            return Result.newFailInstance(e.getMessage());
        }
    }

    /**
     * 供应商驳回
     */
    @RequestMapping("/supplier/reject")
    @ResponseBody
    public Result supplierReject(String declaId, String applyReasonId, String remark) {
        if (StringUtils.isEmpty(declaId)) {
            return Result.newFailInstance("请选择需要处理的报关单");
        }
        if (StringUtils.isEmpty(applyReasonId)) {
            return Result.newFailInstance("请选择驳回原因");
        }
        try {
            return rejectService.supplierReject(declaId, applyReasonId, remark);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("供应商驳回失败", e);
            return Result.newFailInstance(e.getMessage());
        }
    }

    /**
     * 海关退单
     */
    @RequestMapping("/supplier/chargeBack")
    @ResponseBody
    public Result chargeBack(String declaId, String cbReasonId, String remark) {
        if (StringUtils.isEmpty(declaId)) {
            return Result.newFailInstance("请选择需要处理的报关单");
        }
        if (StringUtils.isEmpty(cbReasonId)) {
            return Result.newFailInstance("请选择退单原因");
        }
        try {
            return rejectService.chargeBack(declaId, cbReasonId, remark);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("海关退单失败", e);
            return Result.newFailInstance(e.getMessage());
        }
    }

    /**
     * 查询客户是否申请驳回报关单
     */
    @RequestMapping("/applied")
    @ResponseBody
    public Result applied(String declarationIds) {
        return rejectService.selectByDidsInit(declarationIds);
    }

    /**
     * 报关单驳回原因统计页面
     */
    @RequestMapping("/rejectStatistics")
    public String rejectStatistics() {
        return "/gtimpframe/business/rejectStatistics";
    }

    /**
     * 报关单驳回原因统计
     */
    @RequestMapping("/rejectStatistics/query")
    public void rejectStatisticsQuery(Jqgrid jqgrid, HttpServletResponse response) {
        //查询非海关退单，已驳回的报关单
        jqgrid.addRule(new SearchRule("T_GEN_DICTIONARY_GROUP.CODE", SearchRuleOption.ne, "charge_back_reason"));
        jqgrid.addRule(new SearchRule("T_BIZ_DECLARATION.STATE", SearchRuleOption.eq, Declaration.DeclarationBusinessState.REJECT_STATE));
        declarationService.findRejectList(jqgrid, response);
    }

    /**
     * 查询已驳回单证(客户)
     */
    @RequestMapping("/findRejectList")
    @ResponseBody
    public void findRejectList(Jqgrid jqgrid, HttpServletResponse response) {
        //添加查询条件
        jqgrid.addRule(new SearchRule("STATE", SearchRuleOption.eq, Declaration.DeclarationBusinessState.REJECT_STATE));
        jqgrid.addRule(new SearchRule("CUSTOMER_COM_ID", SearchRuleOption.eq, LoginUtil.getLoginUser().getCompany().getId()));
        jqgrid.addRule(new SearchRule("T_BIZ_DECLA_RELASTIONSHIP.PARENT", SearchRuleOption.inl, ""));
        jqgrid.addRule(new SearchRule("ACCEPTED_RESULT", SearchRuleOption.ni, "NOT_ACCEPTED"));
        declarationService.findRejectList(jqgrid, response);
    }

    /**
     * 查询已驳回单证(供应商)
     */
    @RequestMapping("/findInvalidList")
    @ResponseBody
    public void findInvalidList(Jqgrid jqgrid, HttpServletResponse response) {
        //添加查询条件
        jqgrid.addRule(new SearchRule("STATE", SearchRuleOption.eq, Declaration.DeclarationBusinessState.REJECT_STATE));
        jqgrid.addRule(new SearchRule("SUP_ID", SearchRuleOption.eq, LoginUtil.getLoginUser().getCompany().getId()));
        declarationService.findRejectList(jqgrid, response);
    }

    /**
     * 报关单退单原因统计页面
     */
    @RequestMapping("/chargebackStatistics")
    public String chargebackStatistics() {
        return "/gtimpframe/business/chargebackStatistics";
    }

    /**
     * 报关单退单原因统计
     */
    @RequestMapping("/chargebackStatistics/query")
    public void chargebackStatisticsQuery(Jqgrid jqgrid, HttpServletResponse response) {
        jqgrid.addRule(new SearchRule("T_BIZ_DECLARATION.STATE", SearchRuleOption.eq, Declaration.DeclarationBusinessState.CUSTOM_CHARGEBACK));
        declarationService.findRejectList(jqgrid, response);
    }

    /**
     * 查询客户报关单驳回情况
     */
    @RequestMapping("/rejectRecord")
    @ResponseBody
    public Result rejectRecord(String declarationIds) {
        return rejectService.rejectRecord(declarationIds);
    }

}
